package com.itheima.app.manager;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.autoconfig.HuanXinTemplate;
import com.itheima.domain.db.Announcement;
import com.itheima.domain.db.Question;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Friend;
import com.itheima.domain.vo.AnnouncementVo;
import com.itheima.domain.vo.ContactVo;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.domain.vo.UserCommentVo;
import com.itheima.service.db.AnnouncementService;
import com.itheima.service.db.QuestionService;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.CommentService;
import com.itheima.service.mongo.FriendService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.security.spec.RSAKeyGenParameterSpec;
import java.util.*;

/**
 * @Author 请不要叫我高司令
 * @Date 2022/3/22 14:31
 * @Version 1.0
 */

@Component
public class MessageManager {

    @DubboReference
    private UserInfoService userInfoService;
    @DubboReference
    private QuestionService questionService;

    @DubboReference
    private FriendService friendService;


    @Autowired
    private HuanXinTemplate huanXinTemplate;

    //向陌生人发送一条消息
    public void strangerQuestions(Long jiarenId, String reply) {

        Long userId = UserHolder.get().getId();

        //查询用户昵称
        UserInfo userInfo = userInfoService.findById(userId);
        String nickname = userInfo.getNickname();

        //查询陌生人问题
        Question question = questionService.findByUserId(jiarenId);

        if (question == null) {

            question = new Question();

        }

        //组装json
        Map requestBody = new HashMap<>();

        requestBody.put("userId", userId);
        requestBody.put("HuanXinId", "Hx" + userId);

        requestBody.put("nickname", nickname);
        requestBody.put("strangerQuestion", question.getStrangerQuestion());
        requestBody.put("reply", reply);
        String json = JSON.toJSONString(requestBody);

        //发送环信请求
        huanXinTemplate.sendMsg("HX" + jiarenId, json);


    }

    //添加好友
    public void addContacts(Long friendId) {

        //获取userId
        Long userId = UserHolder.get().getId();

        //rpc互相添加好友
        friendService.addFriend(userId, friendId);

        //环信添加好友
        huanXinTemplate.addContacts("HX" + userId, "HX" + friendId);

    }

    //查询好友
    public ResponseEntity findContactVoByPage(Integer pageNum, Integer pageSize, String keyword) {

        Long userId = UserHolder.get().getId();
        //rpc查询好友分页
        PageBeanVo pageBeanVo = friendService.findFriendByPage(userId, pageNum, pageSize);

        //声明voList
        List<ContactVo> voList = new ArrayList<>();

        List<Friend> friendList = (List<Friend>) pageBeanVo.getItems();
        if (CollectionUtil.isNotEmpty(friendList)) {

            for (Friend friend : friendList) {
                //查询好友信息
                UserInfo userInfo = userInfoService.findById(friend.getFriendId());
                if (StrUtil.isNotEmpty(keyword)) {
                    if (userInfo.getNickname().contains(keyword)) {

                        //创建vo对象
                        ContactVo vo = new ContactVo();
                        vo.setUserInfo(userInfo);
                        vo.setUserId("HX" + friend.getFriendId());//好友的环信id
                        voList.add(vo);

                    }

                } else {

                    ContactVo vo = new ContactVo();
                    vo.setUserInfo(userInfo);
                    vo.setUserId("HX" + friend.getFriendId());//好友的环信id
                    voList.add(vo);

                }

            }
        }
        //将voList设置到分页对象
        pageBeanVo.setItems(voList);

        return ResponseEntity.ok(pageBeanVo);

    }


    @DubboReference
    private CommentService commentService;


    // 查看评论人的信息
    public ResponseEntity findUserCommentVoByPage(Integer commentType, Integer pageNum, Integer pageSize) {
        // 1.获取线程内userId
        Long userId = UserHolder.get().getId();
        // 2.调用rpc查询
        PageBeanVo pageBeanVo = commentService.findUserCommentByPage(userId, commentType, pageNum, pageSize);
        // 3.封装vo
        // 3-1 声明voList
        List<UserCommentVo> voList = new ArrayList<>();
        // 3-2 获取commentList
        List<Comment> commentList = (List<Comment>) pageBeanVo.getItems();
        // 3-3 遍历
        if (CollectionUtil.isNotEmpty(commentList)) {
            for (Comment comment : commentList) {
                // 先查评论人的信息
                UserInfo userInfo = userInfoService.findById(comment.getUserId());
                // 创建vo
                UserCommentVo vo = new UserCommentVo();
                vo.setId(userInfo.getId().toString()); // 操作人id
                vo.setAvatar(userInfo.getAvatar()); // 操作人头像
                vo.setNickname(userInfo.getNickname()); // 操作人昵称
                vo.setCreateDate(DateUtil.formatDateTime(new Date(comment.getCreated()))); // 操作时间
                // 添加vo集合
                voList.add(vo);
            }
        }

        // 4.将volist设置到分页对象
        pageBeanVo.setItems(voList);
        // 5.返回分页对象
        return ResponseEntity.ok(pageBeanVo);
    }


    @DubboReference
    private AnnouncementService announcementService;

    //查询公告
    public ResponseEntity announcements(Integer pageNum, Integer pageSize) {

        //查询公告分页对象
        PageBeanVo pageBeanVo = announcementService.findAnnouncementByPage(pageNum, pageSize);
        //创建vo集合
        List<AnnouncementVo> voList = new ArrayList<>();

        //遍历announcementList封装vo
        List<Announcement> announcementList = (List<Announcement>) pageBeanVo.getItems();

        if (CollectionUtil.isNotEmpty(announcementList)) {

            for (Announcement announcement : announcementList) {

                //封装vo
                AnnouncementVo vo = new AnnouncementVo();
                vo.setId(announcement.getId().toString());
                vo.setTitle(announcement.getTitle());
                vo.setDescription(announcement.getDescription());
                vo.setCreateDate(DateUtil.formatDateTime(announcement.getCreated()));

                voList.add(vo);

            }

        }

        pageBeanVo.setItems(voList);

        return ResponseEntity.ok(pageBeanVo);

    }
}
